//
//  WEBViewController.m
//  Molian
//
//  Created by 胡津 on 15/2/10.
//  Copyright (c) 2015年 xiaocan. All rights reserved.
//

#import "WEBViewController.h"

@interface WEBViewController ()<UMSocialUIDelegate>
@property(nonatomic,strong)UIWebView * webview;//网页
@property(nonatomic,strong)UILabel * Prompt;//
@property BOOL isSpinning;
@property(nonatomic,strong)NSString * ShareURL;
@end

@implementation WEBViewController
@synthesize WEBURL;

-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:YES];
    [HUD hideFromViewAnimated:YES];
  
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = APPCOLORBACKGROUNDCOLOR(1);
    [HUD setOnViewWithTitle:@"请求中" animated:YES];


    
    
    UIBarButtonItem *left1  = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(Back)];
    UIBarButtonItem *left2  = [[UIBarButtonItem alloc]initWithTitle:@"关闭" style:UIBarButtonItemStylePlain target:self action:@selector(btnout)];
    self.navigationItem.leftBarButtonItems = @[left1,left2];//调用↑2个方法
    self.navigationController.interactivePopGestureRecognizer.delegate = self;//解决无法滑动返回

    //网页背部的源
    self.Prompt = [[UILabel alloc]initWithFrame:CGRectMake(0, 65, DeviceWidth, 20)];
    self.Prompt.textAlignment = NSTextAlignmentCenter;
    self.Prompt.textColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    self.Prompt.font = [UIFont fontWithName:FontName size:13];
    [self.view addSubview:self.Prompt];
    
    //webview
    self.webview = [[UIWebView alloc] init];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    if (self.automaticallyAdjustsScrollViewInsets) {
        self.webview.frame = CGRectMake(0, 0, DeviceWidth, DeviceHeight-64);
    }else{
        self.webview.frame = CGRectMake(0, 64, DeviceWidth, DeviceHeight-64);
    }
    self.webview.delegate = self;//因为这个代理设置的self
    self.webview.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.webview];
    self.webview.scalesPageToFit = YES;//自动对页面进行缩放以适应屏幕
    self.webview.suppressesIncrementalRendering = YES;
    [self.webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:WEBURL]]];

    
    //导航栏右边按钮
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"分享" style:UIBarButtonItemStylePlain target:self action:@selector(onNextBtn)];
    
    
}
-(void)Back
{
    //如果可以后退
    if (self.webview.canGoBack == YES) {
        [self.webview goBack];
    }else{
        if (self.navigationController.viewControllers.count>1) {
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [self dismissViewControllerAnimated:YES completion:nil];
        }
    }
}

-(void)btnout
{
    //退出web
    if (self.navigationController.viewControllers.count>1) {
        [self.navigationController popViewControllerAnimated:YES];
    }else{
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

-(void)onNextBtn
{
    //如果需要分享回调,请将delegate对象设置self,并实现下面的回调方法
    [UMSocialSnsService handleOpenURL:[NSURL URLWithString:WEBURL]];
    
    //当前页面的标题
    NSLog(@"当前网页的标题%@  ",self.title);
    //网页地址
    NSLog(@"网页地址 %@" ,self.ShareURL);

    [UMSocialData defaultData].extConfig.wechatSessionData.url = self.ShareURL;
    [[UMSocialData defaultData].urlResource setResourceType:UMSocialUrlResourceTypeImage url:self.ShareURL];

    [UMSocialData defaultData].extConfig.title = self.title;//分享标题
    [UMSocialData defaultData].urlResource.url = self.ShareURL;//分享链接
    [UMSocialData defaultData].extConfig.wechatSessionData.url = self.ShareURL;//微信好友链接
    [UMSocialData defaultData].extConfig.wechatTimelineData.url = self.ShareURL;//朋友圈链接
    [UMSocialData defaultData].extConfig.sinaData.urlResource.url = self.ShareURL;//微博链接
    [UMSocialData defaultData].extConfig.qqData.url = self.ShareURL;//QQ链接
    [UMSocialData defaultData].extConfig.qzoneData.url = self.ShareURL;//QQ空间链接
    [UMSocialData defaultData].extConfig.renrenData.url = self.ShareURL;//人人链接
    [UMSocialData defaultData].extConfig.doubanData.urlResource.url = self.ShareURL;//豆瓣链接
    [UMSocialData defaultData].extConfig.smsData.urlResource.url = self.ShareURL;//短信链接

    //呼出友盟的分享
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:UM_KEY
                                      shareText:[NSString stringWithFormat:@"%@",self.title]
                                     shareImage:[UIImage imageNamed:@"icon"]
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToWechatSession,UMShareToWechatTimeline,UMShareToQQ,UMShareToQzone,UMShareToDouban,UMShareToRenren,UMShareToSms,nil]
                                       delegate:self];
    
    
    
}
-(void)didFinishGetUMSocialDataInViewController:(UMSocialResponseEntity *)response
{
    NSLog(@"didFinishGetUMSocialDataInViewController with response is %@",response);
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    //标题
    self.title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
    
    //网页背部的源
    self.Prompt.text = [NSString stringWithFormat:@"网页由 %@ 提供",request.mainDocumentURL.host];
    
    //分享出去的url
    self.ShareURL = [NSString stringWithFormat:@"%@",[request URL]];

    return true;
}


-(void)webViewDidStartLoad:(UIWebView *)webView
{//当网页视图已经开始加载一个请求后,得到通知。
    if (self.isSpinning == YES) {
        [HUD setOnViewWithTitle:@"请求中" animated:YES];
        self.isSpinning = NO;
    }
    self.title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
}

-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    //当网页视图结束加载一个请求之后,得到通知。
    self.isSpinning = YES;
    [HUD hideFromViewAnimated:YES];
    self.title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
    if (self.webview.canGoBack == YES) {
        UIBarButtonItem *left1  = [[UIBarButtonItem alloc]initWithTitle:@"后退" style:UIBarButtonItemStylePlain target:self action:@selector(Back)];
        UIBarButtonItem *left2  = [[UIBarButtonItem alloc]initWithTitle:@"关闭" style:UIBarButtonItemStylePlain target:self action:@selector(btnout)];
        self.navigationItem.leftBarButtonItems = @[left1,left2];//调用↑2个方法
    }else{
        UIBarButtonItem *left1  = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(Back)];
        UIBarButtonItem *left2  = [[UIBarButtonItem alloc]initWithTitle:@"关闭" style:UIBarButtonItemStylePlain target:self action:@selector(btnout)];
        self.navigationItem.leftBarButtonItems = @[left1,left2];//调用↑2个方法
    }

}
//WebView加载失败的时候调用（请求失败）
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
 {
     [HUD hideFromViewAnimated:YES];
 }
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    if ([self isViewLoaded]&&!self.view.window) {
        self.view = nil;
    }
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
 */

@end
